package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.ProgressStatusForSource;
import com.jilefojing.beans.enums.StatusEnumForPublish;
import com.jilefojing.beans.enums.forattitude.TypeEnumForAttitude;
import com.jilefojing.beans.pojos.DCategory;
import com.jilefojing.beans.pojos.EArticle;
import com.jilefojing.beans.pojos.RCategorySource;
import com.jilefojing.beans.pojos.base.BasePo;
import com.jilefojing.beans.pojos.enhanced.EnhancedAttitude;
import com.jilefojing.beans.pojos.enhanced.EnhancedDCategory;
import com.jilefojing.beans.pojos.enhanced.EnhancedEArticle;
import com.jilefojing.beans.pojos.enhanced.EnhancedRCategorySource;
import com.jilefojing.beans.pojos.enhanced.EnhancedRFavoriteSource;
import com.jilefojing.beans.pojos.enhanced.EnhancedRTagSource;
import com.jilefojing.beans.pojos.enhanced.EnhancedStatistics;
import com.jilefojing.beans.pojos.enhanced.EnhancedTag;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.beans.vo.Order;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IEArticleDao;
import com.jilefojing.service.IAttitudeService;
import com.jilefojing.service.IDCategoryService;
import com.jilefojing.service.IEArticleService;
import com.jilefojing.service.IRCategorySourceService;
import com.jilefojing.service.IRFavoriteSourceService;
import com.jilefojing.service.IRTagSourceService;
import com.jilefojing.service.ISourceService;
import com.jilefojing.service.IStatisticsService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;
import com.jilefojing.util.QiniuUtil;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年10月03日 16时03分48秒
 */
@Service
public class EArticleService extends BaseService<EArticle> implements IEArticleService {
	@Autowired
	private IEArticleDao eArticleDao;
	@Autowired
	private ISourceService sourceService;
	@Autowired
	private IStatisticsService statisticsService;
	@Autowired
	private IRTagSourceService rTagSourceService;
	@Autowired
	private IAttitudeService attitudeService;
	@Autowired
	private IRFavoriteSourceService rFavoriteSourceService;
	@Autowired
	private IDCategoryService dCategoryService;
	@Autowired
	private IRCategorySourceService rCategorySourceService;

	private static final String IMG_PLACEHOLDER = "\\$_PREFIX_\\$";

	@Override
	public List<EnhancedEArticle> getEnhanceds(List<Long> ids, UserContext userContext) {
		List<EArticle> eArticles = eArticleDao.getListByIds(ids);
		if (!Detect.notEmpty(eArticles)) {
			return null;
		}

		List<EnhancedEArticle> enhancedEArticles = this.convent2Enhanceds(eArticles);
		this.appendEnhancedCommons(enhancedEArticles, userContext);
		return enhancedEArticles;
	}

	@Override
	public List<EnhancedDCategory> getEnhancedDCategories(UserContext userContext) {
		List<EnhancedDCategory> topEnhancedDCategories = dCategoryService.getEnhancedTopDCategoriesByItemType(ItemTypeEnumForSource.ARTICLE, userContext);
		dCategoryService.appendEnhancedChildDCategories2EnhancedDCategories(topEnhancedDCategories, userContext);
		return topEnhancedDCategories;
	}

	@Override
	public List<EnhancedEArticle> appendEnhancedCommons(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
		@SuppressWarnings("unchecked")
		List<EnhancedEArticle> enhancedEArticles = (List<EnhancedEArticle>) enhancedItems;

		this.appendEnhancedStatistics(enhancedEArticles, userContext);
		this.appendEnhancedTags(enhancedEArticles, userContext);
		return enhancedEArticles;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EnhancedEArticle> appendEnhancedExtralsForUser(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
		this.appendEnhancedAttitudeForUser((List<EnhancedEArticle>) enhancedItems, userContext);
		this.appendEnhancedRFavoriteSourceForUser((List<EnhancedEArticle>) enhancedItems, userContext);
		return (List<EnhancedEArticle>) enhancedItems;
	}

	@Override
	public EnhancedEArticle appendEnhancedAttitudeForUser(EnhancedEArticle enhancedEArticle, UserContext userContext) {
		this.appendEnhancedAttitudeForUser(Arrays.asList(new EnhancedEArticle[] { enhancedEArticle }), userContext);
		return enhancedEArticle;
	}

	@Override
	public List<EnhancedEArticle> appendEnhancedAttitudeForUser(List<EnhancedEArticle> enhancedEArticles, UserContext userContext) {
		List<Long> audioIds = new ArrayList<Long>();
		for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
			audioIds.add(_EnhancedEArticle.getId());
		}

		List<EnhancedAttitude> enhancedAttitudes = attitudeService.getEnhancedAttitudesByItemsForUser(ItemTypeEnumForSource.ARTICLE, audioIds, TypeEnumForAttitude.PRAISE, userContext);
		if (Detect.notEmpty(enhancedAttitudes)) {
			for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
				for (EnhancedAttitude _EnhancedAttitude : enhancedAttitudes) {
					if (_EnhancedEArticle.getId() == ((EnhancedEArticle) _EnhancedAttitude.getEnhancedItem()).getId()) {
						_EnhancedEArticle.setEnhancedAttitude(_EnhancedAttitude);
						break;
					}
				}
			}
		}
		return enhancedEArticles;
	}

	@Override
	public EnhancedEArticle appendEnhancedRFavoriteSourceForUser(EnhancedEArticle enhancedEArticle, UserContext userContext) {
		this.appendEnhancedRFavoriteSourceForUser(Arrays.asList(new EnhancedEArticle[] { enhancedEArticle }), userContext);
		return enhancedEArticle;

	}

	@Override
	public List<EnhancedEArticle> appendEnhancedRFavoriteSourceForUser(List<EnhancedEArticle> enhancedEArticles, UserContext userContext) {
		List<Long> audioIds = new ArrayList<Long>();
		List<Long> paretnAudioIds = new ArrayList<Long>();

		for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
			if (_EnhancedEArticle.isEntity() && _EnhancedEArticle.getEnhancedParent() != null && _EnhancedEArticle.getEnhancedParent().getId() > 0) {
				paretnAudioIds.add(_EnhancedEArticle.getEnhancedParent().getId());
			} else {
				audioIds.add(_EnhancedEArticle.getId());
			}
		}

		if (Detect.notEmpty(audioIds)) {
			List<EnhancedRFavoriteSource> enhancedRFavoriteSources = rFavoriteSourceService.getEnhancedRFavoriteSourcesByItemsForUser(ItemTypeEnumForSource.ARTICLE, audioIds, userContext);
			if (Detect.notEmpty(enhancedRFavoriteSources)) {
				for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
					if (_EnhancedEArticle.isEntity() && _EnhancedEArticle.getEnhancedParent() != null && _EnhancedEArticle.getEnhancedParent().getId() > 0) {
					} else {
						for (EnhancedRFavoriteSource _EnhancedRFavoriteSource : enhancedRFavoriteSources) {
							if (_EnhancedEArticle.getId() == ((EnhancedEArticle) _EnhancedRFavoriteSource.getEnhancedItem()).getId()) {
								_EnhancedEArticle.setEnhancedRFavoriteSource(_EnhancedRFavoriteSource);
								break;
							}
						}
					}
				}
			}
		}

		if (Detect.notEmpty(paretnAudioIds)) {
			List<EnhancedRFavoriteSource> enhancedRFavoriteSources = rFavoriteSourceService.getEnhancedRFavoriteSourcesByItemsForUser(ItemTypeEnumForSource.ARTICLE, paretnAudioIds, userContext);
			if (Detect.notEmpty(enhancedRFavoriteSources)) {
				for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
					if (_EnhancedEArticle.isEntity() && _EnhancedEArticle.getEnhancedParent() != null && _EnhancedEArticle.getEnhancedParent().getId() > 0) {
						for (EnhancedRFavoriteSource _EnhancedRFavoriteSource : enhancedRFavoriteSources) {
							if (_EnhancedEArticle.getEnhancedParent().getId() == ((EnhancedEArticle) _EnhancedRFavoriteSource.getEnhancedItem()).getId()) {
								_EnhancedEArticle.setEnhancedRFavoriteSource(_EnhancedRFavoriteSource);
								break;
							}
						}
					}
				}
			}
		}

		return enhancedEArticles;
	}

	private void appendEnhancedTags(List<EnhancedEArticle> enhancedEArticles, UserContext userContext) {
		List<Long> articleIds = new ArrayList<Long>();
		for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
			articleIds.add(_EnhancedEArticle.getId());
			if (_EnhancedEArticle.isEntity() && _EnhancedEArticle.getEnhancedParent() != null) {
				articleIds.add(_EnhancedEArticle.getEnhancedParent().getId());
			}
		}

		List<EnhancedRTagSource> enhancedRTagSources = rTagSourceService.getEnhancedRTagSourcesByItems(ItemTypeEnumForSource.ARTICLE, articleIds, userContext);
		if (!Detect.notEmpty(enhancedRTagSources)) {
			return;
		}
		rTagSourceService.appendEnhancedTags2EnhancedRTagSources(enhancedRTagSources, userContext);

		for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
			_EnhancedEArticle.setEnhancedTags(this.getCurEnhancedTags(_EnhancedEArticle, enhancedRTagSources));
		}
	}

	private List<EnhancedTag> getCurEnhancedTags(EnhancedEArticle enhancedEArticle, List<EnhancedRTagSource> enhancedRTagSources) {
		Set<Long> curTagIds = new HashSet<Long>();
		List<EnhancedTag> curEnhancedTags = new ArrayList<EnhancedTag>();
		for (EnhancedRTagSource _EnhancedRTagSource : enhancedRTagSources) {
			long itemId = _EnhancedRTagSource.getEnhancedItem().getDynamicId();
			if (!curTagIds.contains(_EnhancedRTagSource.getEnhancedTag().getId())) {
				if (itemId == enhancedEArticle.getId()) {
					curEnhancedTags.add(_EnhancedRTagSource.getEnhancedTag());
				}
				if (enhancedEArticle.isEntity() && enhancedEArticle.getEnhancedParent() != null && itemId == enhancedEArticle.getEnhancedParent().getId()) {
					curEnhancedTags.add(_EnhancedRTagSource.getEnhancedTag());
				}
				curTagIds.add(_EnhancedRTagSource.getEnhancedTag().getId());
			}
		}
		return curEnhancedTags;
	}

	private void appendEnhancedStatistics(List<EnhancedEArticle> enhancedEArticles, UserContext userContext) {

		List<Long> articleIds = new ArrayList<Long>();
		for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
			articleIds.add(_EnhancedEArticle.getId());
		}
		List<EnhancedStatistics> enhancedStatisticses = statisticsService.getEnhancedStatisticses(ItemTypeEnumForSource.ARTICLE, articleIds, userContext);

		if (Detect.notEmpty(enhancedStatisticses)) {
			for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
				for (EnhancedStatistics _EnhancedStatistics : enhancedStatisticses) {
					if (_EnhancedStatistics.getEnhancedItem() != null) {
						if (_EnhancedEArticle.getId() == ((EnhancedEArticle) _EnhancedStatistics.getEnhancedItem()).getId()) {
							_EnhancedEArticle.setEnhancedStatistics(_EnhancedStatistics);
							break;
						}
					}
				}
			}
		}
	}

	@Override
	public List<EnhancedEArticle> convent2Enhanceds(List<? extends BasePo> pos) {
		@SuppressWarnings("unchecked")
		List<EArticle> eArticles = (List<EArticle>) pos;
		List<EnhancedEArticle> enhancedEArticles = new ArrayList<EnhancedEArticle>();
		for (EArticle _EArticle : eArticles) {
			EnhancedEArticle _EnhancedEArticle = new EnhancedEArticle(_EArticle);
			if (Detect.notEmpty(_EnhancedEArticle.getContent())) {
				_EnhancedEArticle.setContent(_EnhancedEArticle.getContent().replaceAll(IMG_PLACEHOLDER, QiniuUtil.getImagesDomain()));
			}

			enhancedEArticles.add(_EnhancedEArticle);
		}
		return enhancedEArticles;
	}

	@Override
	public Pagination<EnhancedEArticle> getPaginationByCategoryIdsForActive(List<Long> categoryIds, List<ProgressStatusForSource> progressStatuses, Pagination<EnhancedEArticle> pagination,
			UserContext userContext) {
		if (!Detect.notEmpty(pagination.getOrders())) {
			pagination.addOrder(new Order(RCategorySource.COLUMN_SORT, Order.ORDER_DESC));
		}
		@SuppressWarnings("unchecked")
		Pagination<? extends BaseEnhanced> baseEnhancedPagination = sourceService.getPaginationByCategoryIdsForActive(categoryIds,
				Arrays.asList(new ItemTypeEnumForSource[] { ItemTypeEnumForSource.ARTICLE }), Arrays.asList(new StatusEnumForPublish[] { StatusEnumForPublish.ALREADY_RELEASED }), progressStatuses,
				pagination.copy(), userContext);
		pagination.setCount(baseEnhancedPagination.getCount());
		if (!Detect.notEmpty(baseEnhancedPagination.getItems())) {
			return pagination;
		}

		pagination.setItems((List<EnhancedEArticle>) baseEnhancedPagination.getItems());
		return pagination;
	}

	@Override
	public EnhancedEArticle appendEnhancedChildren(EnhancedEArticle enhancedEArticle, UserContext userContext) {
		this.appendEnhancedChildren(Arrays.asList(new EnhancedEArticle[] { enhancedEArticle }), userContext);
		return enhancedEArticle;
	}

	@Override
	public List<EnhancedEArticle> appendEnhancedChildren(List<EnhancedEArticle> enhancedEArticles, UserContext userContext) {
		List<Long> parentIds = new ArrayList<Long>();
		for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
			if (!_EnhancedEArticle.isEntity()) {// 是音频集合
				parentIds.add(_EnhancedEArticle.getId());
			}
		}

		if (Detect.notEmpty(parentIds)) {
			List<EnhancedEArticle> enhancedChildEArticles = this.getEnhancedsByParentIds(parentIds, userContext);
			if (Detect.notEmpty(enhancedChildEArticles)) {
				for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
					if (!_EnhancedEArticle.isEntity()) {
						_EnhancedEArticle.setEnhancedChildren(this.getEnhancedEArticlesForParent(_EnhancedEArticle, enhancedChildEArticles));
					}
				}
			}
		}
		return enhancedEArticles;
	}

	private List<EnhancedEArticle> getEnhancedEArticlesForParent(EnhancedEArticle enhancedParentEArticle, List<EnhancedEArticle> enhancedChildEArticles) {
		List<EnhancedEArticle> curEnhancedChildEArticles = new ArrayList<EnhancedEArticle>();
		for (EnhancedEArticle _EnhancedChildEArticle : enhancedChildEArticles) {
			if (enhancedParentEArticle.getId() == _EnhancedChildEArticle.getEnhancedParent().getId()) {
				curEnhancedChildEArticles.add(_EnhancedChildEArticle);
			}
		}

		return curEnhancedChildEArticles;
	}

	@Override
	public List<EnhancedEArticle> getEnhancedsByParentId(long parentId, UserContext userContext) {
		return this.getEnhancedsByParentIds(Arrays.asList(new Long[] { parentId }), userContext);
	}

	@Override
	public List<EnhancedEArticle> getEnhancedsByParentIds(List<Long> parentIds, UserContext userContext) {
		List<EArticle> eArticles = eArticleDao.getListByParentIds(parentIds);
		if (!Detect.notEmpty(eArticles)) {
			return null;
		}
		List<EnhancedEArticle> enhancedEArticles = this.convent2Enhanceds(eArticles);
		this.appendEnhancedCommons(enhancedEArticles, userContext);
		return enhancedEArticles;
	}

	@Override
	public EnhancedEArticle appendEnhancedDCategories(EnhancedEArticle enhancedEArticle, UserContext userContext) {
		this.appendEnhancedDCategories(Arrays.asList(new EnhancedEArticle[] { enhancedEArticle }), userContext);
		return enhancedEArticle;
	}

	@Override
	public List<EnhancedEArticle> appendEnhancedDCategories(List<EnhancedEArticle> enhancedEArticles, UserContext userContext) {
		List<Long> articleIds = new ArrayList<Long>();
		for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
			articleIds.add(_EnhancedEArticle.getId());
		}
		List<EnhancedRCategorySource> enhancedRCategorySources = rCategorySourceService.getEnhancedsByItems(articleIds, ItemTypeEnumForSource.ARTICLE, userContext);
		rCategorySourceService.appendEnhancedDCategories(enhancedRCategorySources, userContext);
		for (EnhancedEArticle _EnhancedEArticle : enhancedEArticles) {
			List<EnhancedDCategory> _curEnhancedDCategories = this.getCurEnhancedDcategories(_EnhancedEArticle, enhancedRCategorySources);
			_EnhancedEArticle.setEnhancedDCategories(_curEnhancedDCategories);
		}

		return enhancedEArticles;
	}

	private List<EnhancedDCategory> getCurEnhancedDcategories(EnhancedEArticle enhancedEArticle, List<EnhancedRCategorySource> enhancedRCategorySources) {
		for (EnhancedRCategorySource _EnhancedRCategorySource : enhancedRCategorySources) {
			if (_EnhancedRCategorySource.getEnhancedItem().getDynamicId() == enhancedEArticle.getId()) {
				return this.getParseEnhancedDCategories(_EnhancedRCategorySource);
			}
		}
		return new ArrayList<EnhancedDCategory>();
	}

	private List<EnhancedDCategory> getParseEnhancedDCategories(EnhancedRCategorySource enhancedRCategorySource) {
		List<EnhancedDCategory> enhancedDCategories = new ArrayList<EnhancedDCategory>();
		if (enhancedRCategorySource.getEnhancedC1Category() != null && enhancedRCategorySource.getEnhancedC1Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC1Category());
		}
		if (enhancedRCategorySource.getEnhancedC2Category() != null && enhancedRCategorySource.getEnhancedC2Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC2Category());
		}
		if (enhancedRCategorySource.getEnhancedC3Category() != null && enhancedRCategorySource.getEnhancedC3Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC3Category());
		}
		if (enhancedRCategorySource.getEnhancedC4Category() != null && enhancedRCategorySource.getEnhancedC4Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC4Category());
		}
		if (enhancedRCategorySource.getEnhancedC5Category() != null && enhancedRCategorySource.getEnhancedC5Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC5Category());
		}
		if (enhancedRCategorySource.getEnhancedC6Category() != null && enhancedRCategorySource.getEnhancedC6Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC6Category());
		}
		if (enhancedRCategorySource.getEnhancedC7Category() != null && enhancedRCategorySource.getEnhancedC7Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC7Category());
		}
		if (enhancedRCategorySource.getEnhancedC8Category() != null && enhancedRCategorySource.getEnhancedC8Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC8Category());
		}
		if (enhancedRCategorySource.getEnhancedC9Category() != null && enhancedRCategorySource.getEnhancedC9Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC9Category());
		}
		if (enhancedRCategorySource.getEnhancedC10Category() != null && enhancedRCategorySource.getEnhancedC10Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC10Category());
		}
		if (enhancedRCategorySource.getEnhancedC11Category() != null && enhancedRCategorySource.getEnhancedC11Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC11Category());
		}
		if (enhancedRCategorySource.getEnhancedC12Category() != null && enhancedRCategorySource.getEnhancedC12Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC12Category());
		}
		if (enhancedRCategorySource.getEnhancedC13Category() != null && enhancedRCategorySource.getEnhancedC13Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC13Category());
		}
		if (enhancedRCategorySource.getEnhancedC14Category() != null && enhancedRCategorySource.getEnhancedC14Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC14Category());
		}
		if (enhancedRCategorySource.getEnhancedC15Category() != null && enhancedRCategorySource.getEnhancedC15Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC15Category());
		}
		if (enhancedRCategorySource.getEnhancedC16Category() != null && enhancedRCategorySource.getEnhancedC16Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC16Category());
		}
		if (enhancedRCategorySource.getEnhancedC17Category() != null && enhancedRCategorySource.getEnhancedC17Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC17Category());
		}
		if (enhancedRCategorySource.getEnhancedC18Category() != null && enhancedRCategorySource.getEnhancedC18Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC18Category());
		}
		if (enhancedRCategorySource.getEnhancedC19Category() != null && enhancedRCategorySource.getEnhancedC19Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC19Category());
		}
		if (enhancedRCategorySource.getEnhancedC20Category() != null && enhancedRCategorySource.getEnhancedC20Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC20Category());
		}
		if (enhancedRCategorySource.getEnhancedC21Category() != null && enhancedRCategorySource.getEnhancedC21Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC21Category());
		}
		if (enhancedRCategorySource.getEnhancedC22Category() != null && enhancedRCategorySource.getEnhancedC22Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC22Category());
		}
		if (enhancedRCategorySource.getEnhancedC23Category() != null && enhancedRCategorySource.getEnhancedC23Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC23Category());
		}
		if (enhancedRCategorySource.getEnhancedC24Category() != null && enhancedRCategorySource.getEnhancedC24Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC24Category());
		}
		if (enhancedRCategorySource.getEnhancedC25Category() != null && enhancedRCategorySource.getEnhancedC25Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC25Category());
		}
		if (enhancedRCategorySource.getEnhancedC26Category() != null && enhancedRCategorySource.getEnhancedC26Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC26Category());
		}
		if (enhancedRCategorySource.getEnhancedC27Category() != null && enhancedRCategorySource.getEnhancedC27Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC27Category());
		}
		if (enhancedRCategorySource.getEnhancedC28Category() != null && enhancedRCategorySource.getEnhancedC28Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC28Category());
		}
		if (enhancedRCategorySource.getEnhancedC29Category() != null && enhancedRCategorySource.getEnhancedC29Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC29Category());
		}
		if (enhancedRCategorySource.getEnhancedC30Category() != null && enhancedRCategorySource.getEnhancedC30Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC30Category());
		}
		if (enhancedRCategorySource.getEnhancedC31Category() != null && enhancedRCategorySource.getEnhancedC31Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC31Category());
		}
		if (enhancedRCategorySource.getEnhancedC32Category() != null && enhancedRCategorySource.getEnhancedC32Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC32Category());
		}
		if (enhancedRCategorySource.getEnhancedC33Category() != null && enhancedRCategorySource.getEnhancedC33Category().getId() > 0) {
			enhancedDCategories.add(enhancedRCategorySource.getEnhancedC33Category());
		}
		// TODO 可以继续增加 资源类别

		return enhancedDCategories;
	}

	@Override
	public EnhancedEArticle getEnhancedPrevInDCategory(EnhancedDCategory enhancedDCategory, long articleId, List<Order> orders, UserContext userContext) {
		if (!Detect.notEmpty(orders)) {
			orders = new ArrayList<Order>();
			orders.add(new Order(EArticle.COLUMN_SORT, Order.ORDER_DESC));
			orders.add(new Order(EArticle.COLUMN_UPDATE_TIME, Order.ORDER_DESC));
		}
		DCategory dCategory = new DCategory();
		dCategory.setId(enhancedDCategory.getId());
		dCategory.setCkey(enhancedDCategory.getCkey());

		EArticle eArticle = eArticleDao.getPrevInDCategory(dCategory, articleId, orders);
		if (eArticle == null) {
			return null;
		}
		return (EnhancedEArticle) this.convent2Enhanced(eArticle);
	}

	@Override
	public EnhancedEArticle getEnhancedNextInDCategory(EnhancedDCategory enhancedDCategory, long articleId, List<Order> orders, UserContext userContext) {
		if (!Detect.notEmpty(orders)) {
			orders = new ArrayList<Order>();
			orders.add(new Order(EArticle.COLUMN_SORT, Order.ORDER_DESC));
			orders.add(new Order(EArticle.COLUMN_UPDATE_TIME, Order.ORDER_DESC));
		}

		DCategory dCategory = new DCategory();
		dCategory.setId(enhancedDCategory.getId());
		dCategory.setCkey(enhancedDCategory.getCkey());

		EArticle eArticle = eArticleDao.getNextInDCategory(dCategory, articleId, orders);
		if (eArticle == null) {
			return null;
		}
		return (EnhancedEArticle) this.convent2Enhanced(eArticle);
	}
}
